/*
* Copyright (C) 2020 ~ 2024 chaigec All Rights Reserved.
*
* Author:     chaigec <chaigec@163.com>
* Maintainer: chaigec <chaigec@163.com>

 * Use of this source code is governed by MIT license that can be found in the
 * LICENSE file in the root of the source tree. All contributing project authors
 * may be found in the AUTHORS file in the root of the source tree.
*/

#include "MkHttpHookerUt.h"
#include "MkEvent/MkEventPool.h"
#include "MkHttpHooker/MkHttpHooker.h"
#include "MkEvent/MkEvent.h"
#include "MkEvent/MkTimer.h"
#include "MkUtil/MkUtil.h"
#include "MkHttpMedia/MkHttpMediaServer.h"
#include "MkHttpMedia/MkWsFlvPlayer.h"
#include "MkHttp/MkHttpUtil.h"
#include "MkUtil/MkLog.h"
#include "MkMedia/MkFileSource.h"
#include "MkHttp/MkHttpSession.h"
#include "MkEvent/MkTimerPool.h"
#include "MkUtil/MkBase64.h"


void CMkHttpHookerUt::SetUp()
{
    m_pHttpTestServer = nullptr;
    m_pEventPool = nullptr;
    m_pTimer = nullptr;
    m_pHooker = nullptr;

    m_pEventPool = new CMkEventPool;
    m_pEventPool->Start(1);
    m_pTimer = new CMkTimer;
    m_pTimer->StartTimerThread();
    m_WaitHandle = CreateEvent(nullptr, FALSE, FALSE, nullptr);

    m_pHttpTestServer = new CMkHttpApi(m_pEventPool, m_pTimer);
    m_pHttpTestServer->SetWebSocketAccessKey("123");
    m_pHttpTestServer->SetMaxConnections(100);
    m_pHttpTestServer->SetMaxWsConnectorSize(1);
    m_pHttpTestServer->SetUserPass("bbb", "aaa");
    m_pHttpTestServer->SetApiUriPrefix("/api/");
    while (1) {
        m_HttpPort = CMkUtil::GetRandomNumber() % 0xFFFF;
        if (NoneError == m_pHttpTestServer->StartServer(m_HttpPort)) {
            break;
        } else {
            m_pHttpTestServer->StopServer();
        }
    }
    m_pHooker = new CMkHttpHooker(m_pEventPool, m_pTimer);
    m_pHooker->SetHttpUrl("http://127.0.0.1:"+std::to_string(m_HttpPort)+"/api/hooker");
    m_pHooker->SetUserPass("bbb", "aaa");
    MkMapString MapParam;
    MapParam["token"] = "123";
    m_pHooker->SetMapUriParam(MapParam);
    m_pHooker->AddUriParam("test", "kkk");
    m_pHooker->StartSystemStateTimer();
    m_pHooker->SetLocalId("111");
    m_pHooker->SetPayloadCount(1);
}

void CMkHttpHookerUt::TearDown()
{
    m_pHooker->Release();
    CMkThread::ThreadSleep(100);
    m_pTimer->StopTimerThread();
    m_pEventPool->Stop();
    m_pHttpTestServer->StopServer();
    MkDelete(m_pHooker);
    MkDelete(m_pHttpTestServer);
    MkDelete(m_pTimer);
    MkDelete(m_pEventPool);
    CloseHandle(m_WaitHandle);

}

TEST_F(CMkHttpHookerUt, HookerTest)
{
    MkString LocalId = m_pHooker->GetLocalIdId();
    EXPECT_STREQ(LocalId.c_str(), "111");
    MkString HttpUrl = m_pHooker->GetHttpUrl();
    MkString UserName = m_pHooker->GetUserName();
    EXPECT_STREQ(UserName.c_str(), "bbb");
    MkString Pass = m_pHooker->GetPassword();
    EXPECT_STREQ(Pass.c_str(), "aaa");
    MkMapString MapParam = m_pHooker->GetUriParam();
    EXPECT_EQ(2, MapParam.size());
    Json::Value JsonData;
    Uint32 ErrorCode= m_pHooker->GetSystemInfo(JsonData);
    EXPECT_NE(TRUE, JsonData.empty());
    EXPECT_EQ(NoneError, ErrorCode);
    for (int i=0;i<100;i++)
    {
        ErrorCode = m_pHooker->NotifyKeepAlive();
        EXPECT_EQ(NoneError, ErrorCode);
    }
    Json::Value JsonBody;
    JsonBody["aa"] = 1;
    ErrorCode = m_pHooker->NotifyMsg("test", JsonBody);
    EXPECT_EQ(NoneError, ErrorCode);

    ErrorCode = m_pHooker->NotifyMsg("", JsonBody);
    EXPECT_EQ(NoneError, ErrorCode);

    ErrorCode = m_pHooker->AsyncTransparentMessage(JsonBody, [this](Uint32 ErrorCode, const Json::Value &ResBody) {
        EXPECT_EQ(NoneError, ErrorCode);
    });
    EXPECT_EQ(NoneError, ErrorCode);

    ErrorCode = m_pHooker->AsyncSessionActiveRequest("aaa", [this](Uint32 ErrorCode, const Json::Value &ResBody) {
        EXPECT_EQ(NoneError, ErrorCode);
    });
    EXPECT_EQ(NoneError, ErrorCode);

    ErrorCode = m_pHooker->AsyncMsg("axx", JsonBody, [this](Uint32 ErrorCode, const Json::Value &ResBody) {
        EXPECT_EQ(NoneError, ErrorCode);
    });
    EXPECT_EQ(NoneError, ErrorCode);
    m_pHttpTestServer->SetUserPass("", "");
    ErrorCode = m_pHooker->SendRequest(MkHttpMethodGet, "http://127.0.0.1:" + std::to_string(m_HttpPort) + "/api/hooker", MkMapString(), "", MkMapString(), [this](const MkHttpStatusCode& StatusCode, const MkString& Res) {
        EXPECT_EQ(MkHttpStateOk, StatusCode);
    });
    EXPECT_EQ(NoneError, ErrorCode);
}
